package com.das.mechanic_base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.bean.main.DetailByCarBrandAndQuestionBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3DetectionDataView extends View {

    public Context mContext;

    public Paint mPaint;

    public Paint cPaint;

    public Paint tPaint;

    private int lastTextX = -1,lastTextY = -1,lastMiddleX = -1,lastMiddleY = -1;

    /**
     * 显示模式 0 百分比 1 数值
     */
    public int showType,type;

    public List<DetailByCarBrandAndQuestionBean.BrandAmountVoBean> mList;

    private float percent;

    private int textPadding;

    private int center_x,center_y,radius_width;
    private ValueAnimator animator;
    private float lastAngle;

    public X3DetectionDataView(Context context) {
        this(context,null);
    }

    public X3DetectionDataView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3DetectionDataView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mList = new ArrayList<>();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(X3ScreenUtils.dipToPx(24,mContext));

        cPaint = new Paint();
        cPaint.setStyle(Paint.Style.STROKE);
        cPaint.setAntiAlias(true);
        cPaint.setColor(ContextCompat.getColor(mContext, R.color.bg_B1B3BD));
        cPaint.setStrokeWidth(X3ScreenUtils.dip2px(1,mContext) / 2);

        tPaint = new Paint();
        tPaint.setStyle(Paint.Style.FILL);
        tPaint.setAntiAlias(true);
        tPaint.setTextSize(X3ScreenUtils.dipToPx(14,mContext));
        tPaint.setColor(ContextCompat.getColor(mContext, R.color.c333));
        tPaint.setTypeface(ResourcesCompat.getFont(X3MyApp.getInstance().getApp(), R.font.din_text_type));
        tPaint.setTextAlign(Paint.Align.CENTER);

        radius_width = X3ScreenUtils.dipToPx(60,mContext);
        textPadding = X3ScreenUtils.dipToPx(30,mContext);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        center_x = getWidth() / 2;

        center_y = getHeight() / 2;

        RectF rect = new RectF(center_x - radius_width,center_y - radius_width,
                center_x + radius_width,center_y + radius_width);

        lastTextX = -1;
        lastTextY = -1;
        lastMiddleY = -1;
        lastMiddleX = -1;

        for (int i = 0; i < mList.size(); i++) {

            if (i == 0) {
                lastAngle = 0;
            }
            String showText = "";
            switch (type) {
                case 0:
                    showText = (showType == 0) ? (int)(mList.get(i).percentValue * 100 * percent) + "%" : (int)(mList.get(i).carBrandAmount * percent) + "";
                    break;
                case 1:
                    showText = (showType == 0) ? (int)(mList.get(i).percentValue * 100 * percent) + "%" : (int)(mList.get(i).questionToItemAmount * percent) + "";
                    break;
                case 2:
                    showText = (showType == 0) ? (int)(mList.get(i).percentValue * 100 * percent) + "%" : (int)(mList.get(i).questionBaseAmount * percent) + "";
                    break;
            }
            mPaint.setColor(X3StringUtils.getDetectionColor(i));
            // 正常应该从0 开始 但是android系统上的0与时钟上的0不一致 故从-90开始
            canvas.drawArc(rect,-90 + lastAngle, (float) (mList.get(i).percentValue * 360 * percent), false,mPaint);

            double middleAngel = (mList.get(i).percentValue * 360) / 2 + lastAngle - 90;

            // 获取中间坐标
            int middleAngelX = (int) (center_x + (radius_width + X3ScreenUtils.dipToPx(12,mContext)) * Math.cos(Math.toRadians(middleAngel)));
            int middleAngelY = (int) (center_y + (radius_width + X3ScreenUtils.dipToPx(12,mContext)) * Math.sin(Math.toRadians(middleAngel)));

            Path mPath = new Path();
            mPath.moveTo(middleAngelX,middleAngelY);
            double middleXPercent = Math.abs(middleAngelX - center_x) * 100 / center_x * 0.01f;
            double middleYPercent = Math.abs(middleAngelY - center_y) * 100 / center_y * 0.01f;
//            mPath.lineTo((float)(middleAngelX >= center_x ? middleAngelX + ScreenUtils.dipToPx(15,mContext) * middleXPercent : middleAngelX - ScreenUtils.dipToPx(15,mContext) * middleXPercent), (float) (middleAngelY >= center_y ? middleAngelY + ScreenUtils.dipToPx(30,mContext) * middleYPercent : middleAngelY - ScreenUtils.dipToPx(30,mContext)  * middleYPercent));


            int i1 = middleAngelX >= center_x ? middleAngelX + X3ScreenUtils.dipToPx(10, mContext) : middleAngelX - X3ScreenUtils.dipToPx(10, mContext);
            int i2 = middleAngelY >= center_y ? middleAngelY + X3ScreenUtils.dipToPx(10, mContext) : middleAngelY - X3ScreenUtils.dipToPx(10, mContext);

            if (i == 0) {
                lastMiddleX = -1;
                lastMiddleY = -1;
                lastTextY = -1;
                lastTextX = -1;
            }

            int lastX = 0;
            if (lastMiddleY != -1) {
                if (Math.abs(lastMiddleX - middleAngelX) < X3ScreenUtils.dipToPx(18,mContext) && Math.abs(lastMiddleY - middleAngelY) < X3ScreenUtils.dipToPx(18,mContext)) {
                    i1 = i1 < center_x ? i1 - X3ScreenUtils.dipToPx(14,mContext) : i1 + X3ScreenUtils.dipToPx(14,mContext);
//                    lastX = i1 < center_x ? i1 * 2 - center_x : i1 + ScreenUtils.dipToPx(10,mContext);
                    i2 = i2 < center_y ? lastTextY - X3ScreenUtils.dipToPx(14,mContext) : lastTextY + X3ScreenUtils.dipToPx(14,mContext);
                }
            }

            mPath.lineTo(i1, i2);

//            float lastX = middleAngelX >= center_x ? middleAngelX + ScreenUtils.dipToPx(35, mContext) : middleAngelX - ScreenUtils.dipToPx(35, mContext);
//            float lastY = (float) (middleAngelY >= center_y ? middleAngelY + ScreenUtils.dipToPx(30, mContext) * middleYPercent : middleAngelY - ScreenUtils.dipToPx(30, mContext) * middleYPercent);
//            mPath.lineTo(lastX, lastY);

//            if (lastX == 0) {
//                lastX = i1 >= center_x ? i1 + ScreenUtils.dipToPx(10, mContext) : i1 - ScreenUtils.dipToPx(10, mContext);
//            }
            lastX = i1 >= center_x ? i1 + X3ScreenUtils.dipToPx(10, mContext) : i1 - X3ScreenUtils.dipToPx(10, mContext);

            mPath.lineTo(lastX, i2);

            canvas.drawPath(mPath,cPaint);

            int textPointX = (int) (center_x + (radius_width + textPadding) * Math.cos(Math.toRadians((mList.get(i).percentValue * 360) + (-90 + lastAngle - 10))));
            int textPointY = (int) (center_y + (radius_width + textPadding) * Math.sin(Math.toRadians((mList.get(i).percentValue * 360) + (-90 + lastAngle) - 10)));

            Rect textRect = new Rect();
            tPaint.getTextBounds(showText,0,showText.length(),textRect);

//            canvas.drawText(showText,textPointX - ScreenUtils.dipToPx(2,mContext) ,textPointY + ScreenUtils.dipToPx(8,mContext),tPaint);
            canvas.drawText(showText,lastX < center_x ? lastX - textRect.width() / 2 - X3ScreenUtils.dipToPx(5,mContext): lastX + textRect.width() / 2 + X3ScreenUtils.dipToPx(5,mContext),i2 + textRect.height() / 2,tPaint);

            lastAngle += (mList.get(i).percentValue * 360);

            lastMiddleX = middleAngelX;
            lastMiddleY = middleAngelY;
            lastTextX = i1;
            lastTextY = i2;
        }
    }

    /**
     * 显示数据的方法
     * @param showType 0 显示百分比 1 显示数值
     * @param mList
     * @param type 0 标识车型分布 1 表示检测项 2 表示检测点
     */
    public void showDetectionDataView(int showType,int type, List<DetailByCarBrandAndQuestionBean.BrandAmountVoBean> mList) {
        if (X3StringUtils.isListEmpty(mList)) {
            return;
        }
        this.showType = showType;
        this.type = type;
        this.mList = mList;

        animator = ValueAnimator.ofFloat(0,1);
        animator.setStartDelay(100);
        animator.setDuration(800);
        animator.addUpdateListener(animation -> {
            percent = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();

    }

    public void onDestory() {
        if (animator != null) {
            animator.cancel();
        }
    }
}
